<template>
	<view>
		<!-- 我的基本信息 -->
		<view class="myInfo">
			<u-avatar class="headPicture" :src="image" size="80" @click="goToImage"></u-avatar>
			<view style="font-weight: 800;" @click="editName">{{myInfo.groupName}}</view>
			<view style="font-weight: 800;" @click="editName">{{myInfo.position}}-{{myInfo.name}}</view>
			<view style="font-weight: 600;">staffID:{{myInfo.staffId}}</view>
		</view>

		<view class="myInfo">
			<u-avatar class="headPicture" :src="enterprise.image" size="80" @click="goToEImage"></u-avatar>
			<view style="font-weight: 800;" @click="editEName">{{enterprise.name}}</view>
			<view style="font-weight: 600;">enterpriseID:{{enterprise.enterpriseId}}</view>
		</view>


		<view class="option" @click="changeVersion1">
			更改企业
		</view>

		<view class="option" @click="changeVersion">
			切换到个人版
		</view>
		
		<view class="option" @click="backEnterprise">
			退出此企业
		</view>
		
		<view class="option" v-if="admin" @click="delEnterprise">
			解散此企业
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				myInfo: {},
				enterprise: {},
				image: "",
				admin:false
			}
		},
		methods: {
			goToImage() {
				let that = this;
				uni.chooseImage({
					sourceType: ['album'], //从相册选择
					count: 1,
					success(res) {
						let filePath = res.tempFilePaths[0]
						uniCloud.uploadFile({
							filePath: filePath,
							cloudPath: Math.floor(Math.random() * 900000000) + 100000000 + '.jpg',
							success(res) {
								that.myInfo.image = res.fileID;
								that.image = res.fileID;
								uni.setStorageSync("image", res.fileID);
								uni.request({
									url: getApp().url + 'user/update',
									data: {
										userId: uni.getStorageSync("userId"),
										image: res.fileID
									},
									header: {
										token: uni.getStorageSync("token")
									},
									method: "POST",
									success: (res) => {
										console.log(res)
									},
									fail: (error) => {
										console.log(error);
									}
								})
							}
						});
					}
				});
			},
			editName() {
				let that = this;
				uni.showModal({
					title: "请输入要更改的姓名：",
					editable: true,
					success: function(res) {
						if (res.confirm) {
							if (res.content == "") {
								uni.showToast({
									icon: "error",
									title: "姓名为空"
								})
							} else {
								that.myInfo.name = res.content;
								uni.request({
									url: getApp().url + 'staff/changeStaff',
									data: {
										userId: uni.getStorageSync("userId"),
										enterpriseId: uni.getStorageSync("enterprise").enterpriseId,
										name: res.content
									},
									header: {
										token: uni.getStorageSync("token")
									},
									method: "POST",
									success: (response) => {
										if (response.data.code == -1) {
											return uni.showToast({
												icon: "error",
												title: response.data.msg
											});
										};
									},
									fail: (error) => {
										console.log(error);
									}
								})
							}
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			goToEImage() {
				if (uni.getStorageSync("enterprise").adminId == uni.getStorageSync("userId")) {
					let that = this;
					uni.chooseImage({
						sourceType: ['album'], //从相册选择
						count: 1,
						success(res) {
							let filePath = res.tempFilePaths[0]
							uniCloud.uploadFile({
								filePath: filePath,
								cloudPath: Math.floor(Math.random() * 900000000) + 100000000 + '.jpg',
								success(res) {
									that.enterprise.image = res.fileID;
									uni.request({
										url: getApp().url + 'enterprise/changeEnterprise',
										data: {
											enterpriseId: uni.getStorageSync("enterprise")
												.enterpriseId,
											image: res.fileID
										},
										header: {
											token: uni.getStorageSync("token")
										},
										method: "POST",
										success: (res) => {
											console.log(res)
											uni.setStorageSync("enterprise", that.enterprise);
										},
										fail: (error) => {
											console.log(error);
										}
									})
								}
							});
						}
					});
				}
			},
			editEName() {
				if (uni.getStorageSync("enterprise").adminId == uni.getStorageSync("userId")) {
					let that = this;
					uni.showModal({
						title: "请输入要更改的姓名：",
						editable: true,
						success: function(res) {
							if (res.confirm) {
								if (res.content == "") {
									uni.showToast({
										icon: "error",
										title: "姓名为空"
									})
								} else {
									that.enterprise.name = res.content;
									uni.request({
										url: getApp().url + 'enterprise/changeEnterprise',
										data: {
											enterpriseId: uni.getStorageSync("enterprise")
												.enterpriseId,
											name: res.content
										},
										header: {
											token: uni.getStorageSync("token")
										},
										method: "POST",
										success: (response) => {
											if (response.data.code == -1) {
												return uni.showToast({
													icon: "error",
													title: response.data.msg
												});
											};
											uni.setStorageSync("enterprise", that.enterprise);
										},
										fail: (error) => {
											console.log(error);
										}
									})
								}
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					})
				}
			},
			backEnterprise() {
				if(uni.getStorageSync("enterprise").adminId == uni.getStorageSync("userId")){
					return uni.showToast({
						icon:"none",
						title:"您是此企业的管理员，请转让管理员后再退出"
					})
				}
				uni.showModal({
					title: '提示',
					content: '确认要退出此企业吗？',
					success: function(res) {
						if (res.confirm) {
							uni.request({
								url: getApp().url + 'staff/delStaff',
								data: {
									userId: uni.getStorageSync("userId"),
									enterpriseId: uni.getStorageSync("enterprise").enterpriseId
								},
								header: {
									token: uni.getStorageSync("token")
								},
								method: "POST",
								success: (response) => {
									if (response.data.code == -1) {
										return uni.showToast({
											icon: "error",
											title: response.data.msg
										});
									};
							
									uni.showToast({
										icon: "success",
										title: "成功"
									});
							
									uni.reLaunch({
										url: "/pages/person/goToEnterprise/goToEnterprise"
									})
							
								},
								fail: (error) => {
									console.log(error);
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			delEnterprise() {
				uni.showModal({
					title: '提示',
					content: '确认要解散此企业吗？',
					success: function(res) {
						if (res.confirm) {
							uni.request({
								url: getApp().url + 'enterprise/delEnterprise',
								data: {
									enterpriseId: uni.getStorageSync("enterprise").enterpriseId
								},
								header: {
									token: uni.getStorageSync("token")
								},
								method: "GET",
								success: (response) => {
									if (response.data.code == -1) {
										return uni.showToast({
											icon: "error",
											title: response.data.msg
										});
									};
							
									uni.showToast({
										icon: "success",
										title: "成功"
									});
							
									uni.reLaunch({
										url: "/pages/person/goToEnterprise/goToEnterprise"
									})
							
								},
								fail: (error) => {
									console.log(error);
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			changeVersion() {
				uni.setStorageSync("enterprise", {
								enterpriseId: 0
							});
				uni.setTabBarItem({
					index: 0,
					pagePath: "/pages/person/index/index",
					success() {
						uni.switchTab({
							url: "/pages/person/index/index"
						})
					}
				})
				uni.setTabBarItem({
					index: 1,
					pagePath: "/pages/person/contacts/contacts"
				})
				uni.setTabBarItem({
					index: 2,
					pagePath:"/pages/person/find/find",
					text:"发现"
				})
				uni.setTabBarItem({
					index: 3,
					pagePath: "/pages/person/my/my"
				})
			},
			changeVersion1() {
				uni.navigateTo({
					url: "/pages/person/goToEnterprise/goToEnterprise"
				})
			}
		},
		onShow() {
			uni.request({
				url: getApp().url + 'staff/getStaff',
				data: {
					userId: uni.getStorageSync("userId"),
					enterpriseId: uni.getStorageSync("enterprise").enterpriseId
				},
				header: {
					token: uni.getStorageSync("token")
				},
				method: "GET",
				success: (res) => {
					this.myInfo = res.data.data;
				},
				fail: (error) => {
					console.log(error);
				}
			})
			if(uni.getStorageSync("enterprise").adminId == uni.getStorageSync("userId")) {
				this.admin = true;
			} else {
				this.admin = false;
			}
			this.enterprise = uni.getStorageSync("enterprise");
			this.image = uni.getStorageSync("image");
		}
	}
</script>

<style lang="scss">
	.myInfo {
		margin: 10vw 5vw 0vw 5vw;
		padding: 0 5vw 5vw 5vw;
		box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;
		border-radius: 10px;
		display: flex;
		flex-direction: column;
		align-items: center;

		.headPicture {
			position: relative;
			bottom: 5vw;
		}
	}

	.option {
		height: 15vw;
		text-align: center;
		line-height: 15vw;
		margin: 5vw 5vw 0 5vw;
		border-radius: 10px;
		box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;
	}
</style>